// pages/shopinfo/shopinfo.js
var app = getApp();

var that;
var shopid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imglist: [],
    loadingHidden: false,
    autoplay: false,
    interval: 5000,
    duration: 2000,
    showModal: false,
    circular: true,
    shopinfo: {},
    // parductImage:'',
    form: {
      number: 1
    },
    shopcolor: "",
    shopcolorvalue: [],
    listThirdName: "",
    listThirdValue: [],
    listSecondName: '',
    listSecondValue: [],
    firstValue1: "",
    secondValue1: "",
    thirdValue1: "",
    isUseStandard: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.id)
    shopid = options.id
    console.log(shopid + '----商品id---')
    var that = this
    wx.request({ //商品详情接口
      // url: `http://172.188.88.232:8080/hyParductManage/findById?id=${shopid}`,
      url: app.api.getjfshopinfo() + `?id=${shopid}`,
      method: 'get',
      success: function(res) {
        // console.log(wx.getStorageSync("UserData").id)
        console.log(res.data.data)
        var shopinfodata = res.data.data.hyParductManageBean
        var isUseStandardguige = res.data.data.hyParductManageBean.isUseStandard // 
        var color = res.data.data.listFirstName[0]
        var colorvalue = res.data.data.listFirstValue

        var SecondName = res.data.data.listSecondName[0]
        var Secondvalue = res.data.data.listSecondValue

        var ThirdName = res.data.data.listThirdName[0]
        var Thirdvalue = res.data.data.listThirdValue

        console.log(shopinfodata)
        console.log(JSON.parse(shopinfodata.parductImage))
        var imgurl = JSON.parse(shopinfodata.parductImage)
        setTimeout(function() {
          that.setData({
            loadingHidden: true,
            shopinfo: shopinfodata,
            imglist: imgurl,
            shopcolor: color,
            shopcolorvalue: colorvalue,
            listThirdName: ThirdName,
            listThirdValue: Thirdvalue,
            listSecondName: SecondName,
            listSecondValue: Secondvalue,
            isUseStandard: isUseStandardguige,
            detailImg: res.data.data.hyParductManageBean.parductDetails.replace(/\<img/gi, '<img style="width:100%;height:auto" ')
          })
        }, 1500)
      },
    });
  },

  numberSub: function() {
    var t = this.data.form.number;
    if (t <= 1) return !0;
    t--, this.setData({
      form: {
        number: t
      }
    });
  },
  numberAdd: function() {
    var t = this.data.form.number;
    t++, this.setData({
      form: {
        number: t
      }
    });
  },
  numberBlur: function(t) {
    var a = t.detail.value;
    a = parseInt(a), isNaN(a) && (a = 1), a <= 0 && (a = 1), this.setData({
      form: {
        number: a
      }
    });
  },
  addCart: function() {
    this.submit("ADD_CART");
  },
  // buyNow: function() {
  //   this.submit("BUY_NOW");
  // },
  submit: function(t) {
    var a = this;
    if (!a.data.show_attr_picker) return a.setData({
      show_attr_picker: !0
    }), !0;
  },
  radioChange1: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      firstValue1: e.detail.value
    })
  },
  radioChange2: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      secondValue1: e.detail.value
    })
  },
  radioChange3: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      thirdValue1: e.detail.value
    })
  },
  hideAttrPicker: function() {
    this.setData({
      show_attr_picker: !1
    });
  },
  Redeemnow: function() {
    var one = this.data.firstValue1
    var two = this.data.secondValue1
    var third = this.data.thirdValue1
    if (this.data.isUseStandard == 1) {
      if (this.data.shopinfo.parductStock == null || this.data.shopinfo.parductStock == 0 || this.data.shopinfo.parductStock < this.data.form.number) {
        wx.showModal({
          title: '提示',
          content: '该商品库存不足',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else {
              console.log('用户点击取消')
            }
          }
        })


      } else if (one == '') {
        wx.showModal({
          title: '提示',
          content: '请选择商品规格',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else {
              console.log('用户点击取消')
            }

          }
        })
        return false;
      } else if (one !== '' && this.data.shopinfo.parductStock !== null || this.data.shopinfo.parductStock !== 0 || this.data.shopinfo.parductStock > this.data.form.number) {
        that = this
        wx.request({
          url: app.api.getjfGuige() + `?id=${JSON.parse(shopid)}&firstValue=${one}&secondValue=${two}&thirdValue=${third}`, //会员商品管理商品规格提交
          method: 'POST',
          data: {},
          success(res) {
            console.log(res.data)
            console.log(res.data.data.id)
            var guigeidres = res.data.data.id
            wx.navigateTo({
              url: `/page/mine/pages/shopOrder/shopOrder?number=${that.data.form.number}&id=${that.data.shopinfo.id}&Integral=${that.data.shopinfo.parductIntegral}&imgurl=${that.data.shopinfo.parductSimpleImage}&imgname=${that.data.shopinfo.parductName}&shopguigeid=${guigeidres}`,
            })
          }
        })
      }
    } else {
      wx.navigateTo({
        url: `/page/mine/pages/shopOrder/shopOrder?number=${this.data.form.number}&id=${this.data.shopinfo.id}&Integral=${this.data.shopinfo.parductIntegral}&imgurl=${this.data.shopinfo.parductSimpleImage}&imgname=${this.data.shopinfo.parductName}`,
      })
    }

    this.hideAttrPicker()
  },
  showAttrPicker: function() {
    this.setData({
      show_attr_picker: !0
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //小程序触底方法

})